<?php
/*
 *      osmetadictionaryfieldedit.php
 *      
 *      Copyright 2011 ViRak <vetalrakitin@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/oscomponent.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/osbutton.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/osedit.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/oscombobox.php");

class osmetadictionaryfieldedit extends oscomponent {
	
	private $field;
	private $eduuid;
	private $edname;
	private $edlength;
	private $cbtype;
	private $cbdatatype;
	private $fields;
	private $btadd;
	private $btdel;
	private $cbfields;
	private $cblookupobject;
	private $cblookupfield;

	
	function __construct($parent) {
		parent::__construct($parent);
		
		$this->btadd = new osbutton(0);
		$this->btadd->setevent($this, "onclick", "btaddonclick");
		$this->btadd->caption = "Добавить";
		
		$this->btdel = new osbutton(0);
		$this->btdel->setevent($this, "onclick", "btdelonclick");
		$this->btdel->caption = "Удалить";

		$this->eduuid = new osedit(0);
		$this->eduuid->readonly = true;
		
		$this->edname = new osedit(0);
		$this->edname->setevent($this, "onchange", "ednameonchange");
		
		$this->edlength = new osedit(0);
		$this->edlength->setevent($this, "onchange", "edlengthonchange");
		
		$this->cbtype = new oscombobox(0);
		$this->cbtype->setevent($this, "onchange", "cbtypeonchange");
		$this->cbtype->additem("data","Данные");
		$this->cbtype->additem("link","Ссылка");
		
		$this->cblookupobject = new oscombobox(0);
		$this->cblookupobject->setevent($this, "onchange", "cblookupobjectonchange");
		
		$this->cblookupfield = new oscombobox(0);
		$this->cblookupfield->setevent($this, "onchange", "cblookupfieldonchange");
		
		$this->cbdatatype = new oscombobox(0);
		$this->cbdatatype->setevent($this, "onchange", "cbdatatypeonchange");
		$this->cbdatatype->additem("varchar", "Строка");
		$this->cbdatatype->additem("int", "Целое");
		
		$this->cbfields = new oscombobox(0);
		$this->cbfields->setevent($this, "onchange", "cbfieldsonchange");
	}
	
	private function initialize() {
		if ($this->field) {
			$this->eduuid->value = $this->field->uuid;
			$this->edname->value = $this->field->name;
			if (!$this->cbtype->selectitem($this->field->type)) {}
			if ($this->cbtype->selecteditem() == "link") {
				$this->cblookupobject->deleteallitems();
				foreach ($_SESSION["metadata"]->dictionaries->getall() as $dic) {
					$this->cblookupobject->additem($dic->uuid, $dic->name);
				}
				$this->cblookupfield->deleteallitems();
				if ($this->cblookupobject->selectitem($this->field->luobject)) {
					foreach ($_SESSION["metadata"]->dictionaries->byuuid($this->cblookupobject->selecteditem())->fields->getall() as $field) {
						$this->cblookupfield->additem($field->uuid, $field->name);
					}
					$this->cblookupfield->selectitem($this->field->lufield);
				}
			}
			if ($this->cbtype->selecteditem() == "data") {
				$this->cbdatatype->selectitem($this->field->datatype);
				$this->edlength->value = $this->field->length;
			}
			//$this->eduuid->enable();
			//$this->edname->enable();
		} //else {
			//$this->eduuid->value = "";
			//$this->edname->value = "";
			//$this->eduuid->disable();
			//$this->edname->disable();
		//}
		//$this->cbtype->selectitem($this->field->type);
	}
	
	function cblookupobjectonchange($value) {
		$this->field->luobject = $value;
		$this->initialize();
		$this->update();
	}
	
	function cblookupfieldonchange($value) {
		$this->field->lufield = $value;
		$this->initialize();
		$this->update();
	}
	
	function cbdatatypeonchange($value) {
		$this->field->datatype = $value;
		//$this->update();
	}
	
	function cbfieldsonchange($value) {
		$this->field = $this->fields->byuuid($value);
		$this->initialize();
		$this->update();
	}
	
	function cbtypeonchange($value) {
		//if ($value == "link"
		$this->field->type = $value;
		
		$this->initialize();
		$this->update();
	}
	
	function btaddonclick() {
		$this->field = $this->fields->add();
		$this->cbfields->additem($this->field->uuid,$this->field->name);
		//$dic->name = "Имя словаря";
		$this->initialize();
		$this->cbfields->selectitem($this->field->uuid);
		$this->update();
	}
	
	function btdelonclick() {
		$this->fields->delete($this->cbfields->selecteditem());
		$this->cbfields->deleteitem($this->cbfields->selecteditem());
		$this->field = $this->fields->byuuid($this->cbfields->selecteditem());
		$this->initialize();
		$this->update();
	}
	
	function edlengthonchange($value) {
		$this->field->length = $value;
		//$this->update();
	}
	
	function ednameonchange($value) {
		$this->field->name = $value;
		$this->cbfields->edititem($this->field->uuid, $value);
		$this->cbfields->update();
	}
	
	function __set($name, $value) {
		switch ($name) {
			case "fields":{
				if ($value == null) {
					$this->hide();
				} else {
					$this->fields = $value;
					$this->cbfields->deleteallitems();
					foreach ($this->fields->getall() as $field) {
						$this->cbfields->additem($field->uuid,$field->name);
					}
					$this->field = $this->fields->byuuid($this->cbfields->selecteditem());
					$this->initialize();
					$this->show();
				}
				break;
			}
		}
	}
	
	function get() {
		$ret = "<div id=".$this->uuid;
		$ret .= $this->visible?"":" hidden";
		$ret .= ">";
		$ret .= $this->cbfields->get().$this->btadd->get().$this->btdel->get()."<br>";
		if ($this->field) {
			$ret .= "<fieldset><legend>Свойства</legend>";
			$ret .= $this->eduuid->get()."<br>";
			$ret .= $this->edname->get()."<br>";
			$ret .= $this->cbtype->get()."<br>";
			if ($this->cbtype->selecteditem() == "link") {
				$ret .= $this->cblookupobject->get();
				$ret .= $this->cblookupfield->get();
			}
			if ($this->cbtype->selecteditem() == "data") {
				$ret .= $this->cbdatatype->get()."<br>";
				$ret .= $this->edlength->get();
			}
			//$ret .= "<pre>".print_r($this->field,1)."</pre>";
			$ret .= "</fieldset>";
		}
		$ret .= "</div>";
		return $ret;
	}
}
?>
